package com.example.myapplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class WeathAdapter extends RecyclerView.Adapter<WeathAdapter.ViewHolder> {

    private JSONArray list;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView hour;
        TextView weath;
        TextView temperature;

        public ViewHolder (View view) {
            super(view);
            hour = (TextView) view.findViewById(R.id.weath_hour);
            weath = (TextView) view.findViewById(R.id.weath_yu);
            temperature = (TextView) view.findViewById(R.id.weath_temperature);
        }

    }

    public WeathAdapter(JSONArray list){
        this.list = list;
    }

    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weath_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.width=Math.round(parent.getWidth()/5);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        try {
            JSONObject o = list.getJSONObject(position);
            holder.hour.setText(o.getString("hour").substring(8)+"时");
            holder.weath.setText(o.getString("skyconn_desc"));
            holder.temperature.setText(o.getString("temperature")+"℃");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount(){
        return list.length();
    }

}
